package org.garbagecan.springstudy.dao.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test {
	
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"/org/garbagecan/springstudy/dao/jdbc/spring.xml");
		test((UserDao) ctx.getBean("userDao1"));
		test((UserDao) ctx.getBean("userDao2"));
		test((UserDao) ctx.getBean("userDao3"));
		test((UserDao) ctx.getBean("userDao4"));
	}

	private static void test(UserDao userDao) {
		for (int i = 0; i < 10; i++) {
			User user = new User();
			user.setId("" + i);
			user.setName("name_" + i);
			user.setPassword("password_" + i);
			userDao.add(user);

			User user1 = userDao.findById(user.getId());
			System.out.println("user1: " + user1);

			user1.setPassword("123456");
			userDao.update(user1);
			User user2 = userDao.findById(user.getId());
			System.out.println("user2: " + user2);
		}

		List<User> users = userDao.list();
		System.out.println(users);
		for (User user : users) {
			userDao.delete(user);
		}
		users = userDao.list();
		System.out.println(users);
	}
}
